﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Pages;
using Marbles.Resources;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:DynamicContentPlaceHolder runat=server></{0}:DynamicContentPlaceHolder>")]
    public class DynamicContentPlaceHolder : ContentPlaceHolder
    {

        public Unit Width { get { object obj = ViewState["Width"]; return (obj == null) ? Unit.Empty : (Unit)obj; } set { ViewState["Width"] = value; } }
        public Unit Height { get { object obj = ViewState["Height"]; return (obj == null) ? Unit.Empty : (Unit)obj; } set { ViewState["Height"] = value; } }
        public string ContentId { get { object obj = ViewState["ContentId"]; return (obj == null) ? "" : obj.ToString().ToLower(); } set { ViewState["ContentId"] = value.ToLower(); } }

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(this.ContentId)) { this.ContentId = this.ID.ToLower(); }
            Marbles.Core.Pages.Page p = this.Page as Marbles.Core.Pages.Page;
            PageDefinition def = p.CurrentPage;
            bool isEditor = p.IsEditor || p.IsAdministrator;
            LiteralControl content = new LiteralControl();
            content.ID = "content";
            content.Text = "Content Not Found [" + this.ContentId + "]";

            if (def == null) { return; }
            if (def.Content.ContainsKey(this.ContentId))
            {
                content.Text = def.Content[this.ContentId];
            }

            if (isEditor)
            {
                ResourceHandler.RegisterWysiwygScripts(p);
            }

            this.Controls.Add(content);
        }


        protected override void Render(HtmlTextWriter writer)
        {
            Marbles.Core.Pages.Page p = this.Page as Marbles.Core.Pages.Page;
            bool isEditor = p.IsEditor || p.IsAdministrator;
            bool EnableWysiwygEditing = Marbles.Core.Settings.SettingsManager.EnableWysiwygEditing;

            if (isEditor)
            {
                writer.AddAttribute("id", this.ContentId + "_editor");
                writer.AddAttribute("rows", "5");
                writer.AddAttribute("cols", "80");
                writer.AddStyleAttribute("display", "none");
                if (!this.Width.IsEmpty) { writer.AddStyleAttribute("width", this.Width.ToString()); }
                if (!this.Height.IsEmpty) { writer.AddStyleAttribute("height", this.Height.ToString()); } else { writer.AddStyleAttribute("height", "400px"); }
                writer.RenderBeginTag(HtmlTextWriterTag.Textarea);
                base.Render(writer);
                writer.RenderEndTag();
                writer.Write("<a id=\"" + this.ContentId + "_edit\" href=\"javascript:editContent('" + this.ContentId + "'," + EnableWysiwygEditing.ToString().ToLower() + ");\"><img src=\"/Admin/Images/edit-edit.png\" border=\"0\" alt=\"Edit Item\"></a>&nbsp;");
                writer.Write("<a id=\"" + this.ContentId + "_save\" style=\"display:none;\" href=\"javascript:saveContent('" + this.ContentId + "'," + EnableWysiwygEditing.ToString().ToLower() + ");\"><img src=\"/Admin/Images/edit-save.png\" border=\"0\" alt=\"Save Changes\"></a>&nbsp;");
                writer.Write("<a id=\"" + this.ContentId + "_cancel\" style=\"display:none;\" href=\"javascript:cancelEdit('" + this.ContentId + "'," + EnableWysiwygEditing.ToString().ToLower() + ");\"><img src=\"/Admin/Images/edit-cancel.png\" border=\"0\" alt=\"Cancel Changes\"></a>&nbsp;");
            }

            writer.AddAttribute("id", this.ContentId);
            if (!this.Width.IsEmpty) { writer.AddStyleAttribute("width", this.Width.ToString()); }
            if (!this.Height.IsEmpty) { writer.AddStyleAttribute("height", this.Height.ToString()); }
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.Render(writer);
            writer.RenderEndTag();
        }

    }
}
